package com.ruoyi.contract.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.ContractMain;
import com.ruoyi.contract.domain.InvoiceMain;
import com.ruoyi.contract.service.IContractMainService;
import com.ruoyi.contract.service.IInvoiceMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.contract.mapper.CollectionBillMapper;
import com.ruoyi.contract.domain.CollectionBill;
import com.ruoyi.contract.service.ICollectionBillService;

/**
 * 收款管理Service业务层处理
 * 
 * @author 张毅然
 * @date 2021-04-26
 */
@Service
public class CollectionBillServiceImpl implements ICollectionBillService 
{
    @Autowired
    private CollectionBillMapper collectionBillMapper;
    @Autowired
    private IInvoiceMainService invoiceMainService;
    @Autowired
    private IContractMainService contractMainService;
    /**
     * 查询收款管理
     * 
     * @param gid 收款管理ID
     * @return 收款管理
     */
    @Override
    public CollectionBill selectCollectionBillById(String gid)
    {
        return collectionBillMapper.selectCollectionBillById(gid);
    }

    /**
     * 查询收款管理列表
     * 
     * @param collectionBill 收款管理
     * @return 收款管理
     */
    @Override
    public List<CollectionBill> selectCollectionBillList(CollectionBill collectionBill)
    {
        return collectionBillMapper.selectCollectionBillList(collectionBill);
    }

    /**
     * 新增收款管理
     * 
     * @param collectionBill 收款管理
     * @return 结果
     */
    @Override
    public int insertCollectionBill(CollectionBill collectionBill)
    {
        collectionBill.setCreateTime(DateUtils.getNowDate());
        return collectionBillMapper.insertCollectionBill(collectionBill);
    }

    /**
     * 修改收款管理
     * 
     * @param collectionBill 收款管理
     * @return 结果
     */
    @Override
    public int updateCollectionBill(CollectionBill collectionBill)
    {
        collectionBill.setUpdateTime(DateUtils.getNowDate());
        return collectionBillMapper.updateCollectionBill(collectionBill);
    }

    /**
     * 批量删除收款管理
     * 
     * @param gids 需要删除的收款管理ID
     * @return 结果
     */
    @Override
    public int deleteCollectionBillByIds(String[] gids)
    {
        return collectionBillMapper.deleteCollectionBillByIds(gids);
    }

    /**
     * 删除收款管理信息
     * 
     * @param gid 收款管理ID
     * @return 结果
     */
    @Override
    public int deleteCollectionBillById(String gid)
    {
        return collectionBillMapper.deleteCollectionBillById(gid);
    }

    @Override
    public int approveCollectionBillById(String gid) {
        CollectionBill collectionBill = collectionBillMapper.selectCollectionBillById(gid);
        if (collectionBill.getState()!=0){
            throw new CustomException("收款单编号："+collectionBill.getCode()+"已被确认");
        }
        //更新发票收款状态以及收款金额
        String invoiceId = collectionBill.getInvoiceId();
        String contractId = collectionBill.getContractId();
        if(StringUtils.isNotEmpty(invoiceId)){
            InvoiceMain invoiceMain = invoiceMainService.selectInvoiceMainById(invoiceId);
            if (StringUtils.isEmpty(contractId)){
                contractId = invoiceMain.getContractId();
            }
            BigDecimal invoiceAmount = invoiceMain.getInvoiceAmount();
            BigDecimal receivedAmount = invoiceMain.getReceivedAmount();
            receivedAmount = receivedAmount.add(collectionBill.getAmount());
            int compareVal = invoiceAmount.compareTo(receivedAmount);
            invoiceMain.setCollectionState(compareVal+2);
            invoiceMain.setReceivedAmount(receivedAmount);
            invoiceMainService.updateInvoiceMain(invoiceMain);
        }
        //更新合同收款状态以及收款金额
        if (StringUtils.isNotEmpty(contractId)){
            ContractMain contractMain = contractMainService.selectContractMainById(contractId);
            if (contractMain!=null){
                BigDecimal receivedAmount = contractMain.getReceivedAmount();
                BigDecimal contractAmount = contractMain.getContractAmount();
                receivedAmount = receivedAmount.add(collectionBill.getAmount());
                Integer typeFlag = contractMain.getTypeFlag();
                if (typeFlag==0){
                    int compareVal = contractAmount.compareTo(receivedAmount);
                    contractMain.setCollectionState(compareVal+2);
                }else {
                    contractMain.setCollectionState(1);
                }
                contractMain.setReceivedAmount(receivedAmount);
                contractMainService.updateContractMain(contractMain);
            }
        }
        collectionBill.setUpdateTime(DateUtils.getNowDate());
        collectionBill.setUpdateBy( SecurityUtils.getUsername());
        collectionBill.setState(1);
        return collectionBillMapper.updateCollectionBill(collectionBill);
    }
}
